# 二叉树的最小深度

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        return self.depth(root)

    def depth(self, root):
        if not root:
            return 0

        leftTree = self.depth(root.left)
        rightTree = self.depth(root.right)

        if not leftTree and not rightTree:
            return 1
        if not leftTree:
            return rightTree + 1
        if not rightTree:
            return leftTree + 1
        return 1 + min(leftTree, rightTree)
